package de.koller.singularity.client.input;

import de.koller.aframe.util.thread.ICommand;
import de.koller.oceanFramework.tree.Mother;
import de.koller.singularity.SNode;
import de.koller.singularity.client.SClient;
import java.awt.Canvas;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SInput extends SNode {
	
	@Mother public SClient client;

	@Override
	protected void init() {
		super.init();
		
		MouseInput in = new MouseInput();
		Canvas c = client.display.plainDisplay;
		c.addMouseListener( in );
		c.addMouseMotionListener( in );
		c.addMouseWheelListener( in );
		c.addKeyListener( new KeyInput() );
	}
	
	private class KeyInput extends KeyAdapter {

		@Override public void keyPressed( final KeyEvent e ) {
			sroot.call( new KeyPressCall( e.getKeyCode() ) );
		}
				
	}
	
	private class KeyPressCall implements ICommand {
		private final int key;

		public KeyPressCall( int key ) {
			this.key = key;
		}

		@Override
		public void handle( Object... params ) {
			sroot.client.keyPress( key );
		}
	}
	
	private class MouseInput extends MouseAdapter {

		@Override public void mousePressed( MouseEvent e ) {
			sroot.call( new MouseClickCall( client, e.getX(), e.getY() ) );
		}
		
	}
	
}
